package com.zx.pm.modules.zx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.BidBond;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProSupplier;
import com.zx.pm.modules.zx.entity.Supplier;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.ProSupplierService;
import com.zx.pm.modules.zx.service.SupplierService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 项目供应商Controller
 * @author forest
 * @version 2019-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/proSupplier")
public class ProSupplierController extends BaseController{

	@Autowired
	private LzdService lzdService;
	
	@Autowired
	private SupplierService supplierService;
	
	@Autowired
	private ProSupplierService proSupplierService;
	
	@Autowired
	private TenderSectionService tenderSectionService;
	
	@ModelAttribute
	public Lzd get(@RequestParam(required=false) String id) {
		Lzd entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = lzdService.get(id);
		}
		if (entity == null){
			entity = new Lzd();
		}
		return entity;
	}
	
	@RequiresPermissions("zx:proSupplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(Lzd lzd, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Lzd> page = lzdService.findSectionPage(new Page<Lzd>(request, response), lzd); 
		model.addAttribute("page", page);
		return "modules/zx/proSupplierList";
	}

	@RequiresPermissions("zx:proSupplier:view")
	@RequestMapping(value = "form")
	public String form(Lzd lzd, Model model, HttpServletRequest request) {
		ProSupplier ps = new ProSupplier();
		ps.setLzdId(lzd.getId());
		ps.setSectionId(lzd.getSectionId());
		List<ProSupplier> psList = proSupplierService.findList(ps);
		// 获取标段信息
		lzd.setTs(tenderSectionService.get(lzd.getSectionId()));
		lzd.setDepositMoney(lzdService.getDepositMoney(lzd.getId(), lzd.getSectionId()));
		model.addAttribute("lzd", lzd);
		model.addAttribute("psList", psList);
		model.addAttribute("type", request.getParameter("type"));
		return "modules/zx/proSupplierForm";
	}
	
	@RequiresPermissions("zx:proSupplier:edit")
	@RequestMapping(value = "delete")
	public String delete(ProSupplier proSupplier, RedirectAttributes redirectAttributes) {
		proSupplierService.delete(proSupplier);
		addMessage(redirectAttributes, "移除项目供应商信息成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proSupplier/form/?id="+ proSupplier.getLzdId() +"&sectionId="+ proSupplier.getSectionId() +"&repage";
	}
	
	@RequestMapping(value = "mySupplierList")
	public String mySupplierList(HttpServletRequest request, Model model, Supplier supplier) { 
		// 获取供应商列表
		List<Supplier> list = supplierService.findList(supplier);
		// 获取项目供应商列表
		ProSupplier ps = new ProSupplier();
		ps.setLzdId(request.getParameter("lzdId"));
		ps.setSectionId(request.getParameter("sectionId"));
		List<ProSupplier> psList = proSupplierService.findList(ps);
		// 判断供应商是否已经分配给该项目了
		for(Supplier s : list) {
			for(ProSupplier p : psList) {
				if(p.getSupplierId().equals(s.getId())) {
					s.setFenPeiFlag("1");
					break;
				}
			}
		}
		model.addAttribute("lzdId", request.getParameter("lzdId"));
		model.addAttribute("slist", list);
		model.addAttribute("supplier", supplier);
		model.addAttribute("sectionId", request.getParameter("sectionId"));
		return "modules/zx/mySupplierList";
	}
	
	@RequestMapping(value = "savePsRole")
	public String savePsRole(HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String lzdId = request.getParameter("lzdId");
		String sectionId = request.getParameter("sectionId");
		ProSupplier ps = null; 
		// 保存关系逻辑
		String sIds = request.getParameter("sIds");
		String[] arrayId = sIds.split(",");
		for(String str : arrayId) {
			ps = new ProSupplier();
			ps.setSectionId(sectionId);
			ps.setLzdId(lzdId);
			ps.setSupplierId(str);
			proSupplierService.save(ps);
		}
		addMessage(redirectAttributes, "信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proSupplier/form/?id="+ lzdId +"&sectionId="+sectionId+"&repage";
	}
	
	
	@RequiresPermissions("zx:proSupplier:edit")
	@RequestMapping(value = "addProSupplier")
	public String addProExerts(HttpServletRequest request, Model model) { 
		model.addAttribute("lzdId", request.getParameter("lzdId"));
		model.addAttribute("sectionId", request.getParameter("sectionId"));
		return "modules/zx/addSupplier";
	}
	
	
	// 保存项目供应商信息
	@RequestMapping(value = "saveSupplierInfo")
	@ResponseBody
	public String saveSupplierInfo(Supplier supplier){
		// 判断供应商是否存在，如果存在，则不能进行添加
		Supplier s = supplierService.supplierExist(supplier);
		
		ProSupplier ps = new ProSupplier();
		ps.setLzdId(supplier.getLzdId());
		ps.setSectionId(supplier.getSectionId());
		if(s != null && StringUtils.isNotBlank(s.getId())) {
			// 判断关系是否存在
			ps.setSupplierId(s.getId());
			if(proSupplierService.getRelationExits(ps) > 0) {
				return "1";
			}
		}else {
			// 保存供应商信息
			supplier.preInsert();
			ps.setSupplierId(supplier.getId());
			supplierService.saveSupplier(supplier);
		}
		// 保存关系
		proSupplierService.save(ps);
		return "0";
	}
	
	@RequestMapping(value = "saveBidMoney")
	public String saveBidMoney(HttpServletRequest request, RedirectAttributes redirectAttributes, Lzd lzd) {
		if(lzd != null && lzd.getPsList().size() > 0) {
			for(ProSupplier p : lzd.getPsList()) {
				if(p.getBidMoney() == null) {
					break;
				}
				proSupplierService.saveBidMoney(p);
			}
		}
		addMessage(redirectAttributes, "信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proSupplier/form/?id="+ lzd.getId() +"&sectionId="+ lzd.getSectionId() +"&type="+ request.getParameter("type") +"&repage";
	}
}
